package com.xxxt.jxj.controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxt.entity.ClassGrade;
import com.xxxt.entity.College;
import com.xxxt.entity.Grade;
import com.xxxt.entity.School;
import com.xxxt.entity.Specialty;
import com.xxxt.entity.Student;
import com.xxxt.jxj.service.ClassGradeService;
import com.xxxt.jxj.service.CollegeService;
import com.xxxt.jxj.service.GradeService;
import com.xxxt.jxj.service.SchoolService;
import com.xxxt.jxj.service.SpecialtyService;
import com.xxxt.utils.FileUploadUtil;
import com.xxxt.utils.ImportExcelUtil;
import com.xxxt.utils.MultipartFileToFileUtil;
import com.xxxt.utils.UUIDUtil;

@Controller
@RequestMapping("/school")
public class SchoolController {
	@Autowired
	private SchoolService schoolService;
	@Autowired
	private CollegeService collegeService;
	@Autowired
	private SpecialtyService specialtyService;
	@Autowired
	private GradeService gradeService;
	@Autowired
	private ClassGradeService classGradeService;
	/**
	 * 单个学校插入
	 * @param schoolName
	 * @param schoolId
	 * @return
	 */
	@RequestMapping(value="/insertSchool",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject insertSchool(String schoolName){		
		int status=-1;
		String message="";
		JSONObject json = new JSONObject();
		try {
			List<School> selcetAllSchool = schoolService.selcetAllSchool();		
			School school = new School();
			school.setSchoolId(UUIDUtil.getUUID());
			school.setSchoolName(schoolName);
			for (School school1 : selcetAllSchool) {
				if (school1.getSchoolName()==schoolName||school1.getSchoolName().equals(schoolName)) {
					json.put("status", -2);
					json.put("message", "当前学校名称已经有相同的了,请不要重复添加！");
					return json;
				}	
			}
			boolean insertSchool = schoolService.insertSchool(school);
			if (insertSchool) {
				message="添加成功";
				status=0;
			}
			else {
				message="添加失败";
				status=1;					
			}
			json.put("status", status);
			json.put("message", message);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "添加失败");
			return json;
		}
		return json;		
	}
	/**
	 * 学校批量插入
	 * @param school
	 * @return
	 */
	@RequestMapping(value="/batchAddSchool",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject batchAddSchool(@RequestBody Map<String, List> school ){
		JSONObject json = new JSONObject(); 
		try {
			List<School> value=null;
			if (school.size()==0) {
				json.put("status", 1);
				json.put("message", "请不要输入空值");
			}else {
				List<School> selcetAllSchool = schoolService.selcetAllSchool();//school列表
				 Iterator<Entry<String, List>> iterator = school.entrySet().iterator();			
				 while (iterator.hasNext()) {
					Entry<String, List> next = iterator.next();
					value = next.getValue();	
					value = JSON.parseArray(JSON.toJSONString(value),School.class);	//使用json把LinkedHashMap强转为自定义对象		
				 }
				 String schoolId="";
				 for(int i=0;i<value.size();i++){
					 schoolId = value.get(i).getSchoolId();				  
				 for (School a : selcetAllSchool) {
					if (a.getSchoolId()==schoolId|| schoolId.equals(a.getSchoolId())) {
						schoolService.deleteSchool(a.getSchoolId());
					}
				}
				 } 
			}		
			schoolService.batchAddSchool(value);	
			json.put("status", 0);
			json.put("message", "批量插入成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "批量插入失败");
			return json;
		}
		return json;		
	}
	
	/**
	 * 管理员使用excel表格批量导入学校信息
	 * @param excelFile
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/batchAddSchoolByExcel",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject batchAddSchoolByExcel(MultipartFile excelFile) {
		JSONObject json = new JSONObject();
		
		try {
			//判断是否为空
			if (excelFile==null) {
				json.put("status", 1);
				json.put("message", "文件为空");			
			}
			boolean uploadExcel = FileUploadUtil.uploadExcel(FileUploadUtil.getFileNameEnds(excelFile.getOriginalFilename()));
			if(uploadExcel==false) {	
				json.put("status", 2);
				json.put("message", "不支持该类型的文件，请上传表格文件!");		
			}
			File file = MultipartFileToFileUtil.multipartFileToFile(excelFile);
			FileInputStream fileInputStream = new FileInputStream(file);
			
			Map<String,String> hashMap = new HashMap<String,String>();
			hashMap.put("学校ID", "schoolId");
			hashMap.put("学校名称", "schoolName");
			
			List<Map<String,Object>> importExcel = ImportExcelUtil.ImportExcel(fileInputStream, file.getName(), hashMap);
			if (importExcel.size()==0) {
				json.put("status", 3);
				json.put("message", "数据导入错误，不要传入空值");		
			}else {
				List<School> selcetAllSchool = schoolService.selcetAllSchool();//school列表			
				for (int i = 0; i < importExcel.size(); i++) {				
					Map<String, Object> map = importExcel.get(i);				
					String S = (String) map.get("schoolId");//获取value值
					String name = (String) map.get("schoolName");
					for(School school:selcetAllSchool){				
						if (S==school.getSchoolId()||S.equals(school.getSchoolId())) {//与原有列表对比，若有相同ID删除
							schoolService.deleteSchool(school.getSchoolId());
						}
					}			
				}			
				schoolService.batchAddSchoolByExcel(importExcel);//从表格批量添加			
				json.put("status", 0);
				json.put("message", "插入成功");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "插入失败");
			return json;
		}		
		return json;		
	}
	/**
	 * 删除单个学校信息
	 * @param schoolId
	 * @return
	 */
	@RequestMapping(value="/deleteSchool",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject deleteSchool(String schoolId){
		JSONObject json = new JSONObject();
		try {
			boolean deleteSchool = schoolService.deleteSchool(schoolId);
			if (deleteSchool) {
				json.put("status", 0);
				json.put("message", "删除成功");
			}else {
				json.put("status", -1);
				json.put("message", "删除失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "删除失败");
			return json;
		}		
		return json;		
	}
	/**
	 * 修改学校信息
	 * @param schoolId
	 * @param schoolName
	 * @param updateSchoolId
	 * @return
	 */
	@RequestMapping(value="/updateSchool",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject updateSchool(String schoolName,HttpSession session){
		JSONObject json = new JSONObject();
		try {
			School school = (School) session.getAttribute("selcetOneSchool");

			List<School> selcetAllSchool = schoolService.selcetAllSchool();
			for (School school2 : selcetAllSchool) {				
				if (school2.getSchoolName()==schoolName || school2.getSchoolName().equals(schoolName)) {
					json.put("status",3);
					json.put("message", "修改失败，修改的学校名称已经有相同的存在");
					return json;
				}
				}	
					if (schoolName==null) {
						json.put("status", 1);
						json.put("message", "修改失败，请不要修改为空值");
					}
					else {
						boolean updateSchool = schoolService.updateSchool(schoolName,school.getSchoolId());
						if (updateSchool) {
							json.put("status", 0);
							json.put("message", "修改成功");
						}else {
							json.put("status", -1);
							json.put("message", "修改失败");
						}						
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "修改失败");
			return json;
		}		
		return json;		
	}
	/**
	 * 查询单个学校信息
	 * @param schoolId
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/selectOneSchool",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selectOneSchool(String schoolId,HttpSession session){
		JSONObject json = new JSONObject();
		try {
			School selcetOneSchool = schoolService.selcetOneSchool(schoolId);
			if (selcetOneSchool!=null) {
				session.setAttribute("selcetOneSchool", selcetOneSchool);
				json.put("status", 0);
				json.put("message", "查到单个学校信息,保存session");
				json.put("school", selcetOneSchool);
				
			}else {
				json.put("status", -1);
				json.put("message", "查不到");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查不到");
			return json;
		}	
		return json;
	
	}
	/**
	 * 查看学校列表(分页)
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/selcetAllSchoolByPage",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selcetAllSchoolByPage(int pageNum,int pageSize){
		JSONObject json = new JSONObject();
		try {
			PageHelper.startPage(pageNum, pageSize);
			List<School> selcetAllSchoolByPage = schoolService.selcetAllSchoolByPage(pageNum, pageSize);
			if (selcetAllSchoolByPage.size()>0) {
				PageInfo<School> pageInfo=new PageInfo<School>(selcetAllSchoolByPage);
				json.put("status", 0);
				json.put("pageInfo",pageInfo);
				json.put("message","查询成功");
			}else {
				json.put("status", -1);
				json.put("message","查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
			return json;
		}		
		return json;				
	}
	/**
	 * 根据老师和学校查询出对应学生分页
	 * @param schoolId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/selectStudentByTnoAndSchool",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selectStudentByTnoAndSchool(String schoolId ,String tno,int pageNum,int pageSize){
		JSONObject json = new JSONObject();
		try {	
			List<College> selcetAllCollegeBySchoolId = collegeService.selcetAllCollegeBySchoolId(schoolId);
			
			List<Specialty> selcetAllSpecialtyByCollegeId = collegeService.selectStudentByTnoAndSchool(selcetAllCollegeBySchoolId);
			
			List<Grade> selectStudentByTnoAndCollege = specialtyService.selectStudentByTnoAndCollege(selcetAllSpecialtyByCollegeId);
			
			List<ClassGrade> selectStudentByTnoAndSpecialty = gradeService.selectStudentByTnoAndSpecialty(selectStudentByTnoAndCollege);	
			
			if (selcetAllCollegeBySchoolId.size()<=0) {
				json.put("status", -6);
				json.put("message", "当前对应学院没有你的学生");	
				return json;
			}				
			if (selcetAllSpecialtyByCollegeId.size()<=0) {
				json.put("status", -5);
				json.put("message", "当前对应专业没有你的学生");	
				return json;
			}			
			if (selectStudentByTnoAndCollege.size()<=0) {
				json.put("status", -4);
				json.put("message", "当前对应年级没有你的学生");	
				return json;
			}
			if (selectStudentByTnoAndSpecialty.size()<=0) {
				json.put("status", -3);
				json.put("message", "当前对应班级没有你的学生");
				return json;
			}
			PageHelper.startPage(pageNum, pageSize);
			List<Student> selectStudentByTnoAndList = classGradeService.selectStudentByTnoAndList(tno, selectStudentByTnoAndSpecialty,pageNum,pageSize);
			if (selectStudentByTnoAndList.size()>0) {
				PageInfo<Student> pageInfo=new PageInfo<>(selectStudentByTnoAndList);
				json.put("status", 0);
				json.put("message", "查询成功");
				json.put("pageInfo", pageInfo);
			}
			else if (selectStudentByTnoAndList.size()==0) {
				json.put("status", -2);
				json.put("message", "当前没数据");
			}
			else {
				json.put("status", -1);
				json.put("message", "查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
		}
		return json;
	}
}
